﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tigera.Framework.Utility;
using System.IO;

namespace StockMonitor.Configuration
{
    public class CrossSignalRuleConfig
    {
        static CrossSignalRuleConfig()
        {
            ReLoadRules();
        }

        public static List<CrossSignalRule> Rules { get; private set; }

        public static void ReLoadRules()
        {
            List<CrossSignalRule> rules = new List<CrossSignalRule>();

            try
            {
                string txt = File.ReadAllText(System.Windows.Forms.Application.StartupPath + AppMgr.GetValue<string>("CrossSignalRulePath", @"\configure\CrossSignalRulePath.ini", true)).Trim();
                if (txt == string.Empty)
                    Rules = new List<CrossSignalRule>();
                else
                    Rules = Tigera.Framework.SerializerUtility.XmlDeserialize<List<CrossSignalRule>>(txt);
            }
            catch (Exception e)
            {
                //Log.Exception("刷新模型规则失败！", e);
            }
        }

        public static bool SaveRules()
        {
            try
            {
                string txt = Tigera.Framework.SerializerUtility.XmlSerialize(Rules).Trim();
                File.WriteAllText(System.Windows.Forms.Application.StartupPath + AppMgr.GetValue<string>("CrossSignalRulePath", @"\configure\CrossSignalRulePath.ini", true), txt);
                return true;
            }
            catch (Exception e)
            {
                //Log.Exception("保存股票配置失败！", e);
                return false;
            }
        }

        public static List<CrossSignalRule> GetRules(string key)
        {
            var rules = Rules.Where(r => r.Key == key);
            return rules == null ? new List<CrossSignalRule>() : rules.ToList();
        }
    }

    public class CrossSignalRule
    {
        /// <summary>
        /// 规则适用的模型标识
        /// </summary>
        public string Key{get;set;}

        /// <summary>
        /// 下降的，被超越的线
        /// </summary>
        public int GoDownIndex { get; set; }

        /// <summary>
        /// 上升的，超越GoDownIndex的线
        /// </summary>
        public int GoUpIndex { get; set; }

        /// <summary>
        /// 应该发出的信号
        /// </summary>
        public Enums.Signal SendSignal { get; set; }

        /// <summary>
        /// 检查信号规则是否触发
        /// </summary>
        /// <param name="yesterdayCaptial"></param>
        /// <param name="currentCaptial"></param>
        /// <returns></returns>
        public Enums.Signal Check(List<double> lastCaptials, List<double> currentCaptials)
        {
            //比如：GoDownIndex=1（代表5线），GoUpIndex=0（代表3线），SendSignal=买入

            if ((lastCaptials[this.GoDownIndex] > lastCaptials[this.GoUpIndex])           //昨天5线高于昨天3线
                &&                                                              //且
                (currentCaptials[this.GoDownIndex] < currentCaptials[this.GoUpIndex]))     //今天5线低于今天3线
                return this.SendSignal;                                              //说明3上穿5，买入！
            else
                return Enums.Signal.None;                                       //不满足规则则不发信号
        }
    }
}
